package com.example.jzg.mvpdemo.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatEditText;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.jzg.mvpdemo.R;
import com.example.jzg.mvpdemo.Utils.MD5Utils;
import com.example.jzg.mvpdemo.base.BaseActivity;
import com.example.jzg.mvpdemo.bean.LoginBean;
import com.example.jzg.mvpdemo.contract.LoginContract;
import com.example.jzg.mvpdemo.model.LoginModel;
import com.example.jzg.mvpdemo.presenter.LoginPresenter;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author: guochen
 * date: 2016/12/2 17:31
 * email:
 */
public class LoginActivity extends BaseActivity<LoginPresenter, LoginModel> implements LoginContract.loginView {

    @BindView(R.id.username_label)
    ImageView usernameLabel;
    @BindView(R.id.un_clear)
    ImageView unClear;
    @BindView(R.id.username)
    AppCompatEditText username;
    @BindView(R.id.pwd_label)
    ImageView pwdLabel;
    @BindView(R.id.pwd_clear)
    ImageView pwdClear;
    @BindView(R.id.show_pwd)
    ImageView showPwd;
    @BindView(R.id.password)
    AppCompatEditText password;
    @BindView(R.id.login_btn)
    Button loginBtn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.login();
            }
        });

    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }


    @Override
    public void loginSuccess(LoginBean loginBean) {
        if(loginBean.isSuccess()){
            Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, loginBean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void stopLoading() {

    }

    @Override
    public void showError(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public Map<String, String> getParams() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("lgcode", username.getText().toString().trim());
        params.put("pwd", password.getText().toString().trim());
        params.put("op", "login");
        params.put("tokenid", "6");
        Map<String, Object> signMap = new HashMap<>();
        signMap.putAll(params);
        params.put("sign", MD5Utils.getMD5Sign(signMap));
        return params;
    }
}
